<?php
require_once '../includes/functions.php';
check_login();

if (!isset($_GET['subdomain'])) {
    header('Location: index.php');
    exit;
}

$subdomain = $_GET['subdomain'];
$landing_page_path = get_landing_page_path($subdomain);

// 安全检查：确保子域名不包含危险字符
if (!preg_match('/^[a-zA-Z0-9-]+$/', $subdomain)) {
    die('无效的子域名');
}

// 确保目录存在且在正确的位置
if (!file_exists($landing_page_path) || !is_dir($landing_page_path)) {
    header('Location: index.php');
    exit;
}

// 递归删除目录及其内容
function removeDirectory($path) {
    $files = array_diff(scandir($path), array('.', '..'));
    foreach ($files as $file) {
        $fullPath = $path . '/' . $file;
        is_dir($fullPath) ? removeDirectory($fullPath) : unlink($fullPath);
    }
    return rmdir($path);
}

// 删除落地页目录
if (removeDirectory($landing_page_path)) {
    header('Location: index.php?message=' . urlencode('落地页已成功删除'));
} else {
    header('Location: index.php?error=' . urlencode('删除落地页时发生错误'));
}
exit;
